package com.gzhryc.shared_device.oem.code.agent.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentBusinessConfigDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentBusinessConfig;
import com.gzhryc.shared_device.oem.code.config.BusinessConfig;

public class AgentBusinessConfigService {
	
	static Logger log = Logger.getLogger(AgentBusinessConfigService.class);

    public static AgentBusinessConfigService self() {
        return MultiDBTools.getService(AgentBusinessConfigService.class, true);
    }

    public static AgentBusinessConfigService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentBusinessConfigService.class, true);
    }

    AgentBusinessConfigDao dao;

    public AgentBusinessConfigService(String jdbcKey) {
        this.dao = new AgentBusinessConfigDao(jdbcKey);
    }

    public List<AgentBusinessConfig> findByUserId(Long userId){
    	if(userId != null) {
    		Conditions condition = new Conditions(AgentBusinessConfig.class);
    		condition.and().is("userId", userId);
    		
    		try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public List<AgentBusinessConfig> findByUserId(List<Long> userIds){
    	if(userIds != null && userIds.size() > 0) {
    		Conditions condition = new Conditions(AgentBusinessConfig.class);
    		condition.and().in("userId", userIds);
    		
    		try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public AgentBusinessConfig getByKeys(Long userId,String businessType) {
    	if(userId != null && StringTools.isNotBlank(businessType)) {
    		Conditions condition = new Conditions(AgentBusinessConfig.class);
	    	condition.and().is("userId", userId);
	    	condition.and().is("businessType", businessType);
	    	
	    	try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public boolean add(AgentBusinessConfig businessConfig) throws LogicException {
    	if(businessConfig.getServiceChargeRatio() == null) {
    		BusinessConfig config = ConfigUtils.getBusinessConfig();
    		businessConfig.setServiceChargeRatio(config.getServiceChargeRatio());
    	}
    	
    	if(businessConfig.getServiceChargeRatio() < 0 || businessConfig.getServiceChargeRatio() > 100) {
    		throw new LogicException("请设置服务费0-100以内");
    	}
    	
    	businessConfig.setCreateDate(new Date());
    	try {
			return dao.insert(businessConfig) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
    
    public boolean updateNotNull(AgentBusinessConfig businessConfig) throws LogicException {
    	if(StringTools.isNotBlank(businessConfig.getBusinessType()) && businessConfig.getUserId() != null) {
	    	if(businessConfig.getServiceChargeRatio() == null || 
	    			businessConfig.getServiceChargeRatio() < 0 || businessConfig.getServiceChargeRatio() > 100) {
	    		throw new LogicException("请设置服务费0-100以内");
	    	}
	    	Conditions condition = new Conditions(AgentBusinessConfig.class);
	    	condition.and().is("userId", businessConfig.getUserId());
	    	condition.and().is("businessType", businessConfig.getBusinessType());
	    	
	    	try {
				return dao.updateNotNull(businessConfig,condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
    
    public boolean deleteByKeys(Long userId,String businessType) throws LogicException{
    	if(userId != null && StringTools.isNotBlank(businessType)) {
    		Conditions condition = new Conditions(AgentBusinessConfig.class);
	    	condition.and().is("userId", userId);
	    	condition.and().is("businessType", businessType);
	    	
	    	try {
				return dao.deleteByCondition(condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }

}
